package de.dhbw.karlsruhe.picsim.gui;

import de.dhbw.karlsruhe.picsim.gui.control.ComSettings;
import de.dhbw.karlsruhe.picsim.gui.control.ImportLstAction;
import de.dhbw.karlsruhe.picsim.rs232.ComManager;
import de.dhbw.karlsruhe.picsim.rs232.ComManagerImpl;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

/**
 * Created by IntelliJ IDEA.
 * User: goesen
 * Date: Apr 18, 2010
 * Time: 2:13:52 PM
 */
public final class MainWIndowBuilder {
    private static final Log log = LogFactory.getLog(MainWIndowBuilder.class);
    private ComSettings comSettings;


    public void buildMainWindow() {
        final JFrame jFrame = new JFrame();


        final JMenuBar menuBar = new JMenuBar();
        final JMenu fileMenu = new JMenu("File");
        menuBar.add(fileMenu);
        final JMenu com = menuBar.add(new JMenu("COM"));
        final ComManager comManager = new ComManagerImpl();
        comSettings = new ComSettings(jFrame, comManager);
        com.add(comSettings);

        final JButton stepButton = new JButton(new Disabled("Step"));
        menuBar.add(stepButton);

        final JButton playButton = new JButton(new Disabled("Play"));
        menuBar.add(playButton);

        final JButton pauseButton = new JButton(new Disabled("Pause"));
        menuBar.add(pauseButton);

        final JButton resetButton = new JButton(new Disabled("Reset"));
        menuBar.add(resetButton);


        fileMenu.add(new JMenuItem(new ImportLstAction(jFrame, stepButton, playButton, pauseButton, resetButton, comManager)));
        fileMenu.addSeparator();
        final Action beenden = new ProgrammBeenden();
        fileMenu.add(new JMenuItem(beenden));


        jFrame.setJMenuBar(menuBar);
        jFrame.validate();
        jFrame.setTitle("Pic Simulator");
        jFrame.setSize(new Dimension(1000, 500));
        jFrame.setVisible(true);

        jFrame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosed(WindowEvent e) {
                beenden.actionPerformed(null);
            }

            @Override
            public void windowClosing(WindowEvent e) {
                beenden.actionPerformed(null);
            }
        });
    }


    private class ProgrammBeenden extends AbstractAction {
        private ProgrammBeenden() {
            super("Programm beenden");
        }

        @Override
        public void actionPerformed(final ActionEvent e) {
            comSettings.dispose();
            System.exit(0);

        }
    }

    private static class Disabled extends AbstractAction {
        private Disabled(final String s) {
            super(s);
            setEnabled(false);
        }

        @Override
        public void actionPerformed(final ActionEvent e) {


        }
    }
}
